
mui.init();

document.addEventListener("swipeleft", function () {
    var objData = wrongTopicParsingData;
    console.log('前往下一题');
    if (objData.q_index + 1 == objData.question_list.length) {
        mui.toast('已经是最后一题了!', {duration: 1000,type: 'div'});
        return false;
    }
    objData.q_index++; //翻到下一题
});
document.addEventListener("swiperight", function () {
    var objData = wrongTopicParsingData;
    if (objData.q_index == 0) {
        mui.toast('这是第一题哦!', {duration: 1000,type: 'div'});
        return false;
    }
    objData.q_index--; //翻到上一题
});

var que;
var wrongTopicParsingData;

mui.plusReady(function () {    
    var self = plus.webview.currentWebview();
    var quesitionslists = self.data;
    var type = self.type;
    var userInfo = JSON.parse(plus.storage.getItem('userInfo'));
    wrongTopicParsingData = new Vue({
        el: '#jiexi_box',
        data: {
            questionBoxStatus:0,
            questionErrorBoxStatus:0,
            errorTipsContent:"数据加载失败，可能网络异常",
            question_list: [],
            orther_data: [],
            type: 1,
            question_id: '',
            q_index: 0,
            collection_status: 0,
            con: '',
            yes_num: 300,
            notes_list: '',
        },
        watch: {
            q_index: function (val, oldVal) {
                console.log(val);
                if (val + 1 == this.question_list.length) {
                    mui.toast('已经是最后一题了!', {duration: 1000,type: 'div'});
                    return false;
                }
                this.question_id = this.question_list[val].id;
                this.type = this.question_list[val].type;
                this.collection_status = this.question_list[val].collection_status;
                this.notes_list = this.question_list[val].notes;
            },
            con: function () {
                var str = this.con;
                var len = str.replace(/[\u0391-\uFFE5]/g, "aa").length;
                this.yes_num = 300 - len;
            },
        },
        mounted() {
            this.getWrongTopicParsingData();
        },
        methods: {
            clickRefresh:function(){
                this.getWrongTopicParsingData();
            },
            getWrongTopicParsingData: function () {
                var self = this;
                showWatting();
                var api_host = plus.storage.getItem('HOST_URL');
                $.ajax({
                    url: api_host + '/api/questionbank/choice_question/get_kg_jx',
                    data: {
                        'data': quesitionslists,
                        'type': type,
                    },
                    headers:{'User-Token':plus.storage.getItem('UserToken')},
                    timeout:3000,
                    dataType: 'json',
                    type: 'post',
                    success: function (msg) {
                        if(msg.code == 1){
                            self.questionErrorBoxStatus = 0;
                            self.questionBoxStatus = 1;
                            self.question_list = msg.data.question;
                            self.orther_data = msg.data.other;
                            self.question_id = msg.data.question[0].id;
                            self.type = msg.data.question[0].type;
                            self.notes_list = msg.data.question[0].notes;
                            self.collection_status = msg.data.question[0].collection_status;
                        }else{
                            self.questionErrorBoxStatus = 1;
                            self.questionBoxStatus = 0;
                            self.errorTipsContent = msg.msg;
                            mui.toast(msg.msg, {duration: '200',type: 'div'});
                        }
                    },
                    error: function (xhr, type, errorThrown) {
                        self.questionErrorBoxStatus = 1;
                        mui.toast('加载失败!', {duration: '200',type: 'div'});
                    },
                    complete: function () {
                        plus.nativeUI.closeWaiting();
                    }
                });
            },
            add_collection: function () {
                questionCollection(this.question_id, this.question_list[this.q_index]['category'],this);
            },
            open_jc: function () {
                //打开纠错界面
                mui.openWindow({
                    url: 'errorCorrection.html',
                    extras: {
                        'question_id': this.question_id,
                        'category': this.question_list[this.q_index]['category']
                    },
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                })
            },
            add_user_notes: function () {
                $('.popMask').show();
                $('.popNotes').animate({
                    'bottom': '0%'
                }, 300).show();
            },
            yc_notes: function () {
                //隐藏答题卡
                $('.popMask').hide();
                $('.popNotes ,.popNotes3').animate({
                    'bottom': '-100%'
                }, 300).hide(300);
            },
            tijiao_notes: function () {
                var self = this;
                $('.popMask').hide();
                $('.popNotes').animate({
                    'bottom': '-100%'
                }, 300).hide(300);
                var api_host = plus.storage.getItem('HOST_URL');
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: api_host + "/api/questionbank/public/add_question_notes",
                    headers:{'User-Token':plus.storage.getItem('UserToken')},
                    timeout:3000,
                    data: {
                        'question_id': self.question_id,
                        'category': self.question_list[self.q_index]['category'],
                        'content': self.con
                    },
                    success: function (msg) {
                        mui.toast(msg.msg, {
                            duration: '3000',
                            type: 'div'
                        });
                    },
                    error: function () {
                        mui.toast('提交笔记错误请重试', {
                            duration: '3000',
                            type: 'div'
                        });
                    }
                });
            },
            //修改笔记
            modify_user_notes: function () {
                $('.popMask').show();
                $('.popNotes3').animate({
                    'bottom': '0%'
                }, 300).show();
            },
            queren_notes: function () {
                $('.popMask').hide();
                $('.popNotes3').animate({
                    'bottom': '-100%'
                }, 300).hide(300);
            },
        }
    })
});
